package com.younion.task;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.younion.business.IMemberCardService;
import com.younion.business.IRechargeRuleService;
import com.younion.vo.MemberCardVO;
import com.younion.vo.RechargeFeedbackTaskLogVO;
import com.younion.vo.RechargeRuleVO;

/**
 *  
 * 
 * @author Administrator
 */
public class RechargeFeekbackTask extends Task{

	private static Logger            logger          = LoggerFactory.getLogger(RechargeFeekbackTask.class);
	
	private static final String TASKTYPE = "FEEKBACKTASK";
	
	@Autowired
	private IMemberCardService memberCardService = null;

	@Autowired
	private IRechargeRuleService rechargeRuleService = null;
	
	@Override
	public void work() {
		
		try {
			List<MemberCardVO> membercards = memberCardService.selectByFeedbackCount(Maps.newHashMap());
			List<Map<String,Object>> maps = Lists.newArrayList();
			List<RechargeFeedbackTaskLogVO> logs = Lists.newArrayList();
			logger.error("There are "+membercards.size() + " fired membercards, begin to handle one by one");
			for (MemberCardVO m : membercards) {
				 RechargeRuleVO rule = rechargeRuleService.getById(m.getFiredRuleId().toString());
				 Map updateCardParam = Maps.newHashMap();
				 if(m.getFeedBackCount() != null && m.getFeedBackCount() < rule.getGiveCount() 
						 && isFeedBackTime(m, rule) ){
					 Integer times = m.getFeedBackCount()+1;
					 updateCardParam.put("cardBalance", m.getCardBalance() + rule.getEveryGiveMoney());
					 updateCardParam.put("feedBackCount", times);
					 updateCardParam.put("id", m.getId());
					 updateCardParam.put("lastFeedBackDate", new Date());
					 RechargeFeedbackTaskLogVO ruleLog = new RechargeFeedbackTaskLogVO(m, rule, rule.getEveryGiveMoney(), 0, times ,new Date());
					 maps.add(updateCardParam);
					 logs.add(ruleLog);
					 memberCardService.updateRechargeFeekback(maps, logs);
				 }
			}

		} catch (Exception e) {
			logger.error("Recharge Feedback Task Error");
			logger.error(e.toString());
		}
		
	}
	
	private boolean isFeedBackTime(MemberCardVO m, RechargeRuleVO rule ){
		if(m.getLastFeedBackDate() == null ) return true;
		LocalDate begin = new LocalDate(m.getLastFeedBackDate());
		LocalDate end = new LocalDate(new Date());
		int days = Days.daysBetween(begin, end).getDays(); 
		if(days < rule.getFrequency())
			return true;
		return false;
	}

	public IMemberCardService getMemberCardService() {
		return memberCardService;
	}

	public void setMemberCardService(IMemberCardService memberCardService) {
		this.memberCardService = memberCardService;
	}

	public IRechargeRuleService getRechargeRuleService() {
		return rechargeRuleService;
	}

	public void setRechargeRuleService(IRechargeRuleService rechargeRuleService) {
		this.rechargeRuleService = rechargeRuleService;
	}
	
}
